package com.erp.erp_ui.warehouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.business.req.ImportCarryDetailInfoReq;
import com.erp.erp_entitys.warehouse.req.SellOutStockInfoByPageReq;
import com.erp.erp_entitys.warehouse.req.sellOutStockStatistics1Req;
import com.erp.erp_entitys.warehouse.rsp.sellOutStockStatistics1Rsp;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.req.SellOutStockAddReq;
import com.erp.erp_ui.warehouse.service.SellOutStockService;
import com.erp.erp_ui.warehouse.vo.ImportCarryDetailOfOutStockVo;
import com.erp.erp_ui.warehouse.vo.ImportCarryProductOfOutStockVo;
import com.erp.erp_ui.warehouse.vo.SellOutStockPageInfoVo;
import com.erp.erp_ui.warehouse.vo.SellOutStockVo;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : SellOutStockController
 * @Description : 销售出库控制层
 * @Author : lst
 * @Date: 2020-08-17 19:29
 */
@Controller
@RequestMapping(value = "sellOutStock")
public class SellOutStockController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(SellOutStockController.class);

    /**
     * 销售出库业务逻辑接口
     */
    private final SellOutStockService sellOutStockService;

    @Autowired
    public SellOutStockController(SellOutStockService sellOutStockService) {
        this.sellOutStockService = sellOutStockService;
    }

    /**
     * 首页-分页列表查询
     *
     * @param req 查询参数
     * @return 报价信息
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(SellOutStockInfoByPageReq req) {
        try {
            LOGGER.info("销售出库-首页-分页列表查询 req=" + req.toString());
            PageInfo<SellOutStockPageInfoVo> pageInfo = sellOutStockService.getListByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("销售出库-首页-分页列表查询异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 新增-导出运明细分页查询
     * @param req 查询参数
     * @return 分页结果
     */
    @GetMapping("/add/getCarryDetailInfoByPage")
    @ResponseBody
    public R getCarryDetailInfoByPage(ImportCarryDetailInfoReq req){
        try {
            LOGGER.info("销售出库-新增-导出运明细分页查询 req=" + req.toString());
            PageInfo<ImportCarryDetailOfOutStockVo> pageInfo = sellOutStockService.getCarryDetailInfo(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("销售出库-新增-导出运明细分页查询异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增-导出运明细分页查询-通过出运明细id获取出运明细产品信息
     * @param id 出运明细id
     * @return 分页结果
     */
    @GetMapping("/add/getCarryDetailChildInfo")
    @ResponseBody
    public R getCarryDetailChildInfo(Integer id){
        try {
            LOGGER.info("销售出库-新增-导出运明细分页查询-通过出运明细id获取出运明细产品信息 id=" + id);
            List<ImportCarryProductOfOutStockVo> stockVos = sellOutStockService.getCarryDetailProductList(id);
            return R.build(stockVos);
        }catch (Exception e){
            LOGGER.info("销售出库-新增-导出运明细分页查询-通过出运明细id获取出运明细产品信息异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 销售出库新增
     * @param req 新增对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@Validated @RequestBody SellOutStockAddReq req){
        try {
            LOGGER.info("仓库管理-销售出库-新增、修改 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return sellOutStockService.add(req, name, code);
        }catch (Exception e){
            LOGGER.info("仓库管理-销售出库-新增、修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 详情
     * @param id 详情id
     * @return 详情信息
     */
    @GetMapping("/detail")
    @ResponseBody
    public R detail(Integer id){
        try {
            LOGGER.info("仓库管理-销售出库-详情：id=" + id);
            SellOutStockVo detailVo = sellOutStockService.detail(id);
            return R.build(detailVo);
        }catch (Exception e){
            LOGGER.info("仓库管理-销售出库-详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除
     * @param req 删除条件
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody OperationReq req) {
        try {
            LOGGER.info("仓库管理-销售出库-删除 req=" + req);
            sellOutStockService.delete(req);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("仓库管理-销售出库-删除异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 数据操作接口
     * @param req 操作对象
     * @return 操作结果
     */
    @PostMapping("/operation")
    @ResponseBody
    public R operation(@RequestBody OperationReq req) {
        try {
            LOGGER.info("仓库管理-销售出库-数据操作接口 req=" + req);
            return sellOutStockService.operation(req);
        } catch (Exception e) {
            LOGGER.info("仓库管理-销售出库-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 销售出库产品列表统计-分页
     * @param req 操作对象
     * @return 操作结果
     */
    @RequestMapping("sellOutStockStatistics1ListByPage")
    @ResponseBody
    public R sellOutStockStatistics1ListByPage(sellOutStockStatistics1Req req) {
        try {
            LOGGER.info("仓库管理-销售出库-数据操作接口 req=" + req);
            PageInfo<sellOutStockStatistics1Rsp> pageInfo=sellOutStockService.sellOutStockStatistics1ListByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-销售出库-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 销售出库产品列表统计--导出
     * @param req 操作对象
     * @return 操作结果
     */
    @RequestMapping("sellOutStockStatistics1Export")
    @ResponseBody
    public R sellOutStockStatistics1Export(sellOutStockStatistics1Req req) {
        try {
            LOGGER.info("仓库管理-销售出库-销售出库产品列表统计--导出 req=" + req);
            JSONObject info = sellOutStockService.sellOutStockStatistics1Export(req);
            return R.build(info);
        } catch (Exception e) {
            LOGGER.info("仓库管理-销售出库-销售出库产品列表统计--导出异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


}
